package com.business.api.official.toursism.websiteimage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.api.official.toursism.websiteimage.dto.WebsiteImageApiDTO;
import com.business.api.official.toursism.websiteimage.from.WebsiteImageApiSaveFrom;
import com.business.api.official.toursism.websiteimage.from.WebsiteImageApiUpdateFrom;
import com.business.api.official.toursism.websiteimage.manager.WebsiteImageApiManager;
import com.business.api.official.toursism.websiteimage.query.WebsiteImageApiPageQuery;
import com.business.api.official.toursism.websiteimage.query.WebsiteImageApiQuery;
import com.business.api.official.toursism.websiteimage.service.WebsiteImageApiService;
import com.business.api.official.toursism.websiteimage.vo.WebsiteImageApiBriefPageVO;
import com.business.api.official.toursism.websiteimage.vo.WebsiteImageApiDetailListVO;
import com.business.api.official.toursism.websiteimage.vo.WebsiteImageApiDetailPageVO;
import com.business.api.official.toursism.websiteimage.vo.data.WebsiteImageApiBriefData;
import com.business.api.official.toursism.websiteimage.vo.data.WebsiteImageApiDetailData;
import com.business.system.official.toursism.websiteimage.entity.WebsiteImage;
import com.business.system.official.toursism.websiteimage.service.WebsiteImageService;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.dto.SeedMessageDTO;
import com.seed.core.pojo.service.impl.SeedApiServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description 轮播图
 * @author CodeGenerator
 * @date   2025-01-27
 * @version V1.0
 */
@Slf4j
@Service("websiteImageApiService")
@Transactional(rollbackFor = ServiceException.class)
public class WebsiteImageApiServiceImpl extends SeedApiServiceImpl<WebsiteImageApiManager, WebsiteImage> implements WebsiteImageApiService {

    @Autowired
    private WebsiteImageService systemService;


    /**
     * 持久化
     *
     * @param websiteImageFrom 需要保存的对象
     * @return id，如果保存失败返回空字符串
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteImageApiDTO save(WebsiteImageApiSaveFrom websiteImageFrom) throws ServiceException {
        WebsiteImageApiDTO apiDTO = new WebsiteImageApiDTO();
        WebsiteImage websiteImage = new WebsiteImage();
        try {
            BeanUtils.copyProperties(websiteImageFrom, websiteImage, "id");
        } catch (Exception e) {
            throw new ServiceException(e, websiteImageFrom);
        }
        SeedMessageDTO baseSeedDTO = this.save(websiteImage);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        apiDTO.setWebsiteImage(websiteImage);
        return apiDTO;
    }

    /**
     * 部分更新
     *
     * @param websiteImageFrom 需要更新的对象
     * @return 是否成功
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteImageApiDTO updateSelectiveById(WebsiteImageApiUpdateFrom websiteImageFrom) throws ServiceException {
        WebsiteImageApiDTO apiDTO = new WebsiteImageApiDTO();
        if (StringUtils.isEmpty(websiteImageFrom.getId())) {
            apiDTO.setCode("A0410");
            apiDTO.setMessage("更新主键为空");
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        WebsiteImage websiteImage = new WebsiteImage();
        try {
            BeanUtils.copyProperties(websiteImageFrom, websiteImage);
        } catch (Exception e) {
            throw new ServiceException(e, websiteImageFrom);
        }
        SeedMessageDTO baseSeedDTO = this.updateById(websiteImage);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        return apiDTO;
    }

    /**
     * 获取全部
     *
     * @param query 查询条件
     * @return 详情VOList
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteImageApiDetailListVO listAll(WebsiteImageApiQuery query) throws ServiceException {
        WebsiteImage queryEntity = new WebsiteImage();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteImage> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        List<WebsiteImage> list = this.list(lambdaQueryWrapper);
        List<WebsiteImageApiDetailData> detailVOList;
        try {
            detailVOList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteImageApiDetailListVO(detailVOList);
    }

    /**
     * 简略对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的简略对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteImageApiBriefPageVO getBriefPageVO(WebsiteImageApiPageQuery query) throws ServiceException {
        Page<WebsiteImage> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteImage queryEntity = new WebsiteImage();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteImage> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteImage> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteImage> list = pageData.getRecords();
        List<WebsiteImageApiBriefData> voList;
        try {
            voList = baseManager.listConvertApiBriefData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteImageApiBriefPageVO(page, voList);
    }

    /**
     * 详细对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的详细对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteImageApiDetailPageVO getDetailPageVO(WebsiteImageApiPageQuery query) throws ServiceException {
        Page<WebsiteImage> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteImage queryEntity = new WebsiteImage();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteImage> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteImage> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteImage> list = pageData.getRecords();
        List<WebsiteImageApiDetailData> voList;
        try {
            voList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteImageApiDetailPageVO(page, voList);
    }

}